package com.leonzhangxf.guice.bill;

import com.google.inject.*;
import com.google.inject.matcher.Matchers;
import com.leonzhangxf.guice.interceptor.BillIntercept;
import com.leonzhangxf.guice.interceptor.BillInterceptor;

import javax.inject.Singleton;
import java.util.Map;

/**
 * bill module
 *
 * @author leonzhangxf
 */
public class BillModule extends AbstractModule {

    @Override
    protected void configure() {
        // 最基本的DI配置
        bind(BillingService.class).to(BillingServiceImpl.class).in(Scopes.SINGLETON);
        // 抽象工厂+单例方式
        bind(TransactionLog.class).toProvider(DatabaseTransactionLogProvider.class).in(Scopes.SINGLETON);

        // 注入配置
        bind(String.class).annotatedWith(ApiKey.class).toInstance("test paypal api key value");

        // AOP拦截器注入
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(BillIntercept.class), new BillInterceptor());

        bind(new TypeLiteral<Map<String, String>>() {})
            .annotatedWith(ConfigMap.class)
            .toProvider(ConfigProvider.class).in(Scopes.SINGLETON);
    }

    /**
     * 工厂方法+单例方式提供bean
     */
    @Provides
    @Singleton
    CreditCardProcessor providePayPalCreditCardProcessor(@ApiKey String apiKey,
                                                         @ConfigMap Map<String, String> config) {
        return new PaypalCreditCardProcessor(apiKey, config);
    }
}
